package bookez.view.bean.backing;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIInput;
import javax.faces.component.UIMessage;

import bookez.model.businessobject.Route;
import bookez.model.exception.BusinessLogicLayerException;
import bookez.model.exception.RouteExistingException;
import bookez.view.bean.RouteBean;
import bookez.view.util.ProcessMessage;


@ManagedBean
@SessionScoped
public class RouteForm extends BaseBackingBean {

	
	public void createRoute() {
		
		try {
			
			String fromCity = (String)getFromCityInput().getValue();
			String toCity = (String)getToCityInput().getValue();
			
			getRouteBean().setFromCity( fromCity );
			getRouteBean().setToCity( toCity );
			
			Route route = RouteBean.createRoute( getRouteBean() );
			boolean isSaved = getRouteBean().getService().saveRoute( route );
			currentRoute = route.getId();
			if ( isSaved ) {
				ProcessMessage.processInfo( formMessage.getClientId(), RouteBean.CREATE_SUCCEEDED, null );
			}
		}//end try
		catch ( RouteExistingException ree ) {
			ProcessMessage.processError( formMessage.getClientId(), RouteBean.ROUTE_EXISTING, null );
			ree.printStackTrace();
		}//end catch RouteExistingException
		
		catch ( BusinessLogicLayerException bll ) {
			ProcessMessage.processError( formMessage.getClientId(), RouteBean.CANNOT_CREATE, null );
			bll.printStackTrace();
		}//end catch
		
	}//end method createRoute
	
	public void updateRoute() {
		try {
			
			String fromCity = (String)getFromCityInput().getValue();
			String toCity = (String)getToCityInput().getValue();
			
			getRouteBean().setFromCity( fromCity );
			getRouteBean().setToCity( toCity );
			
			Route route = RouteBean.createRoute( getRouteBean() );
			route.setId( this.currentRoute );
			boolean isUpdated = getRouteBean().getService().saveRoute( route );
			if ( isUpdated ) {
				ProcessMessage.processInfo( this.formMessage.getClientId(), RouteBean.CREATE_SUCCEEDED, null );
			}
		}//end try
		
		catch ( BusinessLogicLayerException bll ) {
			ProcessMessage.processError( this.formMessage.getClientId(), RouteBean.CANNOT_CREATE, null );
			bll.printStackTrace();
		}//end catch
	}//end method updateRoute
	
	public void findRoute() {
		try {
			
			String fromCity = (String)getFromCityInput().getValue();			
			List<Route> result = getServiceLocator().getRouteService().getRouteByFromCity( fromCity );
			setRoutes( result );
		}//end try
		
		catch ( BusinessLogicLayerException bll ) {
			bll.printStackTrace();
		}//end catch
	}//end method findRoute
	
	public Route getSelectedRoute() {
		Route route = RouteBean.createRoute( getRouteBean() );
		return route;
	}//end getSelectedRoute
	
	
	public void setFromCityInput(UIInput fromCityInput) { this.fromCityInput = fromCityInput; }
	public UIInput getFromCityInput() { return fromCityInput; }

	public void setToCityInput(UIInput toCityInput) { this.toCityInput = toCityInput; }
	public UIInput getToCityInput() { return toCityInput; }
	
	public UIMessage getFormMessage() { return this.formMessage; }
	public void setFormMessage( UIMessage value ) { this.formMessage = value; }
	
	public RouteBean getRouteBean() { return routeBean; }
	public void setRouteBean( RouteBean value ) { this.routeBean = value; }
	
	public List<Route> getRoutes() { return this.routes; }
	public void 	 	setRoutes( List<Route> value) { this.routes = value; }
	
	
	
	private UIInput fromCityInput;
	private UIInput toCityInput;
	private UIMessage formMessage;
	
	@ManagedProperty ( name="routeBean", value = "#{routeBean}")
	private RouteBean routeBean;
	private Long currentRoute;
	private List<Route> routes;
	
	private static final long serialVersionUID = 5209494361622993778L;
}//end method RouteForm
